package com.example.test_client;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import android.util.Log;

public class TCPClient  {

	Socket socket;

	/* public void run() {
		 if (connect("192.168.14.213", 4444)==0){
			 if(sendMessage("HEYYYYYYY")==0){
				 closeConnection();
			 }
		 }
	 }*/
	
	/*
	 * public void run() { try { Socket socket = new Socket("192.168.14.213",
	 * 4444); Log.d("TCP", "C: Connected"); String message =
	 * "Hello from Client android emulator";
	 * 
	 * try { Log.d("TCP", "C: Sending: '" + message + "'"); PrintWriter out =
	 * new PrintWriter( new BufferedWriter( new
	 * OutputStreamWriter(socket.getOutputStream())),true);
	 * out.println(message); Log.d("TCP", "C: Sent."); Log.d("TCP", "C: Done.");
	 * 
	 * } catch(Exception e) { Log.e("TCP", "S: Error", e);
	 * 
	 * } finally { socket.close(); }
	 * 
	 * } catch (Exception e) { Log.e("TCP", "C: Error", e); } }
	 */

	public int connect(String Ip, int port) {
		try {
			socket = new Socket(Ip, port);
			Log.d("TCP", "C: Connected");
		}

		catch (Exception e) {
			return -1;
		}

		return 0;
	}

	public int sendMessage(String message) {
		try {
			Log.d("TCP", "C: Sending: '" + message + "'");
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())), true);
			out.println(message);
			Log.d("TCP", "C: Sent.");
			Log.d("TCP", "C: Done.");

		} catch (Exception e) {
			Log.e("TCP", "S: Error", e);
			return -1;
		}
		return 0;
	}
	
	public int closeConnection() {
		try{
		socket.close();
		}		
		catch(Exception e){
			return -1;
		}
		return 0;
	}
	
}

	
